package com.szw.dlyk.dto;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.szw.dlyk.domain.Permission;
import com.szw.dlyk.domain.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @program: dlyk
 * @description: 用户登录DTO
 * @author: Songzw
 * @create: 2024-01-21 19:14
 **/
@EqualsAndHashCode(callSuper = true)
@Data
public class UserDtoLogin extends User implements UserDetails {

    /**
     * 角色list
     */
    private List<String> roleList;

    /**
     * 权限标识list
     */
    private List<String> permissionList;

    /**
     * 菜单的List
     */
    private List<Permission> menuPermissionList;
    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List< GrantedAuthority> authorities = new ArrayList<>();
        if (!ObjectUtil.isEmpty(this.getRoleList())){
            //角色
            this.getRoleList().forEach(role -> authorities.add(new SimpleGrantedAuthority(role)));
        }

        if (!ObjectUtil.isEmpty(this.getPermissionList())){
            //权限标识符
            this.getPermissionList().forEach(permission -> authorities.add(new SimpleGrantedAuthority(permission)));

        }
        return authorities;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.getLoginPwd();
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return this.getLoginAct();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return this.getAccountNoExpired() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return this.getAccountNoLocked() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return this.getCredentialsNoExpired() == 1;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return this.getAccountEnabled() == 1;
    }
}
